<?php
/**
 * 被投企业信息
 */
//传人基金id（基金基本信息表）
function beitouqiye_create($fund_id){
	  //我们为这个页面设置标题
	 drupal_set_title('新增被投企业信息');
	 $page_content = l('查看被投企业列表','beitouqiye/fundlist/'.$fund_id) . '<br />'. '<br />';
	 $render_array = array(
	   '#markup' => $page_content, 
	 );
	 
	 $form = drupal_get_form('beitouqiye_form',$fund_id);
	 $render_array['#markup'] .= drupal_render($form);
	 
	 return $render_array;
}

function beitouqiye_form($form, &$form_state, $fund_id){
	 $form['bid'] = array('#type' => 'hidden', '#value' => $fund_id);
	 
	 $form['tips'] = array(
	  '#prefix' =>'<div id="tips">',
	  '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
	  '#suffix' =>'</div>',
	 );
	 
	 $form['company_name'] = array(
	  '#title' => t('被投企业名称'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['company_name'])?$_SESSION['beitouqiye_form']['company_name']:"",
	 );
	 $today = getdate();
     $form['touzi_timestamp'] = array(
	     '#type' => 'date', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
	     '#title' => t('投资时间'),
	     '#default_value' => array('year' =>  $today['year'], 'month' =>  $today['mon'], 'day' => $today['mday']), 
	 );
	 $form['touzi_money'] = array(
	  '#title' => t('投资额（万元）'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['touzi_money'])?$_SESSION['beitouqiye_form']['touzi_money']:"",
	  '#description' => t('例如：250.55'),
	 ); 
	 $form['qiye_gubenshu'] = array(
	  '#title' => t('企业总股本数'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['qiye_gubenshu'])?$_SESSION['beitouqiye_form']['qiye_gubenshu']:"",
	 ); 
	 $form['shougou_gubenshu'] = array(
	  '#title' => t('收购股本数'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['shougou_gubenshu'])?$_SESSION['beitouqiye_form']['shougou_gubenshu']:"",
	 ); 
	 $form['zhangu_percentage'] = array(
	  '#title' => t('占股比例(%)'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['zhangu_percentage'])?$_SESSION['beitouqiye_form']['zhangu_percentage']:"",
	  '#description' => t('例如：16.8'),
	 ); 
	 //企业情况
	  $form['zuce_money'] = array(
	  '#title' => t('企业注册资本(万元)'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['zuce_money'])?$_SESSION['beitouqiye_form']['zuce_money']:"",
	 );
	  $form['locality'] = array(
	  '#title' => t('企业所属地区'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['locality'])?$_SESSION['beitouqiye_form']['locality']:"",
	 );
	  $form['field'] = array(
	  '#title' => t('企业所属行业'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['field'])?$_SESSION['beitouqiye_form']['field']:"",
	 );
	 $today = getdate();
     $form['sheli_timestamp'] = array(
	     '#type' => 'date', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
	     '#title' => t('设立时间'),
	     '#default_value' => array('year' =>  $today['year'], 'month' =>  $today['mon'], 'day' => $today['mday']), 
	  );
	 $form['fazhanjieduan'] = array(
	  '#title' => t('企业所属发展阶段'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['fazhanjieduan'])?$_SESSION['beitouqiye_form']['fazhanjieduan']:"",
	 );
	 $form['qianyinian_allmoney'] = array(
	  '#title' => t('投资前一年度末总资产(万元)'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['qianyinian_allmoney'])?$_SESSION['beitouqiye_form']['qianyinian_allmoney']:"",
	 );	 
	 $form['qianyinian_jingmoney'] = array(
	  '#title' => t('投资前一年度末净资产(万元)'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['qianyinian_jingmoney'])?$_SESSION['beitouqiye_form']['qianyinian_jingmoney']:"",
	 );
	 $form['qianyinian_salemoney'] = array(
	  '#title' => t('投资前一年度末销售收入(万元)'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['qianyinian_salemoney'])?$_SESSION['beitouqiye_form']['qianyinian_salemoney']:"",
	 );
	  $form['qianyinian_profitmoney'] = array(
	  '#title' => t('投资前一年度末净利润(万元)'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['qianyinian_profitmoney'])?$_SESSION['beitouqiye_form']['qianyinian_profitmoney']:"",
	 );
     //企业联系人
	 $form['lianxiren_name'] = array(
	  '#title' => t('企业联系人'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['lianxiren_name'])?$_SESSION['beitouqiye_form']['lianxiren_name']:"",
	 );	 
	 $form['lianxiren_phone'] = array(
	  '#title' => t('电话'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['lianxiren_phone'])?$_SESSION['beitouqiye_form']['lianxiren_phone']:"",
	 );
	 $form['lianxiren_mail'] = array(
	  '#title' => t('邮箱'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['lianxiren_mail'])?$_SESSION['beitouqiye_form']['lianxiren_mail']:"",
	 );
	  $form['lianxiren_mobile'] = array(
	  '#title' => t('手机'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['lianxiren_mobile'])?$_SESSION['beitouqiye_form']['lianxiren_mobile']:"",
	 );
	 $form["shifou_gaoxingqiye"] = array(
	    '#title' => t('是否高新技术企业'),
		"#type" => "checkbox",
		//"#prefix" => "<td>是否高新技术企业</td><td>",
		//"#suffix" => "</td><tr>",
		'#required' => FALSE,
	  	'#default_value' =>isset($_SESSION['beitouqiye_form']['shifou_gaoxingqiye'])?$_SESSION['beitouqiye_form']['shifou_gaoxingqiye']:"",
	);
	
  	 $form['main_business'] = array(
	  '#title' => t('企业主营业务'),
	  '#type' => 'textarea',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['main_business'])?$_SESSION['beitouqiye_form']['main_business']:"",
	 );
	 $form['remark'] = array(
	  '#title' => t('备注'),
	  '#type' => 'textarea',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['remark'])?$_SESSION['beitouqiye_form']['remark']:"",
	 );
	
	  $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('确认'),
	  );
	  return $form;
}

function beitouqiye_form_validate($form, &$form_state){
	  if (drupal_strlen($form_state['values']['touzi_money']) > 15) {
	    form_set_error('touzi_money', '要求精度小于15 个字符');
	  } 
	  if (!($form_state['values']['touzi_money']>=0)) {
	    form_set_error('touzi_money', '请输入正数');
	  } 
	  
	  if (!($form_state['values']['qiye_gubenshu']>=0)) {
	    form_set_error('qiye_gubenshu', '请输入正数');
	  } 
	  
	  if (!($form_state['values']['zhangu_percentage']>0)) {
	    form_set_error('zhangu_percentage', '请输入正数');
	  } 	  
}

function beitouqiye_form_submit($form, &$form_state){
	  //把表单的值存放在会话中去，由于这里涉及到了两个不同的表单之间传值。
	 $_SESSION['beitouqiye_form'] = $form_state['values'];
	 //表单重定向到确认页面
	 $form_state['redirect'] = 'beitouqiye/confirm';
}
 
function beitouqiye_confirm_page(){
  //我们为这个页面设置标题
	 drupal_set_title('新增被投企业信息');
	 
	 //这里首先作了判断，如果会话中没有设置contactus_form，返回contactus
	 if(empty($_SESSION['beitouqiye_form'])){
	  drupal_goto('beitouqiye/fundlist');
	 }	
	 $render_array = array(
	   '#markup' => '', 
	 );
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('beitouqiye_confirm_form');
	 $render_array['#markup'] .= drupal_render($form);
	 //Drupal7的页面回调，返回的应该是一个数组
	 return $render_array;
}

function beitouqiye_confirm_form(){
     
     $form['company_name'] = array(
	  '#title' => t('被投企业名称'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['company_name'])?$_SESSION['beitouqiye_form']['company_name']:"",
	 );
	 $touzi_timestamp = $_SESSION['beitouqiye_form']['touzi_timestamp'];
     $form['touzi_timestamp'] = array(
	     '#type' => 'date', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
	     '#title' => t('被投时间'),
	     '#markup' => isset($_SESSION['beitouqiye_form']['touzi_timestamp'])?$touzi_timestamp['year'].'年'.$touzi_timestamp['month'].'月'.$touzi_timestamp['day'].'日':"", 
	 );
	 $form['touzi_money'] = array(
	  '#title' => t('投资额（万元）'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['touzi_money'])?$_SESSION['beitouqiye_form']['touzi_money']:"",
	  //'#description' => t('例如：250.55'),
	 ); 
	 $form['qiye_gubenshu'] = array(
	  '#title' => t('企业总股本数'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['qiye_gubenshu'])?$_SESSION['beitouqiye_form']['qiye_gubenshu']:"",
	 ); 
	 $form['shougou_gubenshu'] = array(
	  '#title' => t('收购股本数'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['shougou_gubenshu'])?$_SESSION['beitouqiye_form']['shougou_gubenshu']:"",
	 ); 
	 $form['zhangu_percentage'] = array(
	  '#title' => t('占股比例'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['zhangu_percentage'])?$_SESSION['beitouqiye_form']['zhangu_percentage']:"",
	  '#description' => t('例如：16.8'),
	 ); 
	 //企业情况
	  $form['zuce_money'] = array(
	  '#title' => t('企业注册资本(万元)'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['zuce_money'])?$_SESSION['beitouqiye_form']['zuce_money']:"",
	 );
	  $form['locality'] = array(
	  '#title' => t('企业所属地区'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['locality'])?$_SESSION['beitouqiye_form']['locality']:"",
	 );
	  $form['field'] = array(
	  '#title' => t('企业所属行业'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['field'])?$_SESSION['beitouqiye_form']['field']:"",
	 );
	 $sheli_timestamp = $_SESSION['beitouqiye_form']['sheli_timestamp'];
     $form['sheli_timestamp'] = array(
	     '#type' => 'date', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
	     '#title' => t('设立时间'),
	     '#markup' =>  isset($_SESSION['beitouqiye_form']['sheli_timestamp'])?$sheli_timestamp['year'].'年'.$sheli_timestamp['month'].'月'.$sheli_timestamp['day'].'日':"", 
	  );
	 $form['fazhanjieduan'] = array(
	  '#title' => t('企业所属发展阶段'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['fazhanjieduan'])?$_SESSION['beitouqiye_form']['fazhanjieduan']:"",
	 );
	 $form['qianyinian_allmoney'] = array(
	  '#title' => t('投资前一年度末总资产(万元)'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['qianyinian_allmoney'])?$_SESSION['beitouqiye_form']['qianyinian_allmoney']:"",
	 );	 
	 $form['qianyinian_jingmoney'] = array(
	  '#title' => t('投资前一年度末净资产(万元)'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['qianyinian_jingmoney'])?$_SESSION['beitouqiye_form']['qianyinian_jingmoney']:"",
	 );
	 $form['qianyinian_salemoney'] = array(
	  '#title' => t('投资前一年度末销售收入(万元)'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['qianyinian_salemoney'])?$_SESSION['beitouqiye_form']['qianyinian_salemoney']:"",
	 );
	  $form['qianyinian_profitmoney'] = array(
	  '#title' => t('投资前一年度末净利润(万元)'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['qianyinian_profitmoney'])?$_SESSION['beitouqiye_form']['qianyinian_profitmoney']:"",
	 );
     //企业联系人
	 $form['lianxiren_name'] = array(
	  '#title' => t('企业联系人'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['lianxiren_name'])?$_SESSION['beitouqiye_form']['lianxiren_name']:"",
	 );	 
	 $form['lianxiren_phone'] = array(
	  '#title' => t('电话'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['lianxiren_phone'])?$_SESSION['beitouqiye_form']['lianxiren_phone']:"",
	 );
	 $form['lianxiren_mail'] = array(
	  '#title' => t('邮箱'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['lianxiren_mail'])?$_SESSION['beitouqiye_form']['lianxiren_mail']:"",
	 );
	  $form['lianxiren_mobile'] = array(
	  '#title' => t('手机'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['lianxiren_mobile'])?$_SESSION['beitouqiye_form']['lianxiren_mobile']:"",
	 );
	 $form["shifou_gaoxingqiye"] = array(
	    '#title' => t('是否高新技术企业'),
		"#type" => "checkbox",
		//"#prefix" => "<td>是否高新技术企业</td><td>",
		//"#suffix" => "</td><tr>",
		'#required' => FALSE,
	  	'#markup' =>isset($_SESSION['beitouqiye_form']['shifou_gaoxingqiye'])?$_SESSION['beitouqiye_form']['shifou_gaoxingqiye']:"",
	);
	
  	 $form['main_business'] = array(
	  '#title' => t('企业主营业务'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['main_business'])?$_SESSION['beitouqiye_form']['main_business']:"",
	 );
	 $form['remark'] = array(
	  '#title' => t('备注'),
	  '#type' => 'item',
	  '#markup' =>isset($_SESSION['beitouqiye_form']['remark'])?$_SESSION['beitouqiye_form']['remark']:"",
	 );

	  $form['back'] = array(
	    '#type' => 'submit',
	    '#value' => t('返回'),
	    '#submit' => array('beitouqiye_confirm_form_back'),
	  );
	
	  $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('提交'),
	  );
	  
	  return $form;
}

function beitouqiye_confirm_form_back($form, &$form_state){
 $form_state['redirect'] = 'beitouqiye/create/'.$_SESSION['beitouqiye_form']['bid'];
}

function beitouqiye_confirm_form_submit($form, &$form_state){
		/**
	   * 写入数据库
	   */
	  $values = NULL;
	  global  $user;
	  if(empty($_SESSION['beitouqiye_form'])){
		  drupal_goto('beitouqiye/create/'.$values['bid']);
	  }else{
		  $values = $_SESSION['beitouqiye_form'];
		  unset($_SESSION['beitouqiye_form']);
	  }
      $touzi_timestamp = mktime(0, 0, 0,$values['touzi_timestamp']['month'], $values['touzi_timestamp']['day'], $values['touzi_timestamp']['year']);
	  $sheli_timestamp = mktime(0, 0, 0,$values['sheli_timestamp']['month'], $values['sheli_timestamp']['day'], $values['sheli_timestamp']['year']);
	  $last_id = db_insert('beitouqiye_info')
  		->fields(array(
  		     'fund_id' => $values['bid'],
  		     'user_id' => $user->uid,
   			 'company_name' => $values['company_name'],
             'touzi_timestamp' => $touzi_timestamp,
   			 'touzi_money' => $values['touzi_money'],
   			 'qiye_gubenshu' => $values['qiye_gubenshu'],
   			 'shougou_gubenshu' => $values['shougou_gubenshu'],
   			 'zhangu_percentage' => $values['zhangu_percentage'],		 
			 'zuce_money' => $values['zuce_money'],
   			 'locality' => $values['locality'],
   			 'field' => $values['field'],
             'sheli_timestamp' => $sheli_timestamp,  			 
   			 'fazhanjieduan' => $values['fazhanjieduan'],
   			 'qianyinian_allmoney' => $values['qianyinian_allmoney'],
   			 'qianyinian_jingmoney' => $values['qianyinian_jingmoney'],
   			 'qianyinian_salemoney' => $values['qianyinian_salemoney'],
   			 'qianyinian_profitmoney' => $values['qianyinian_profitmoney'],
   			 'lianxiren_name' => $values['lianxiren_name'],
   			 'lianxiren_phone' => $values['lianxiren_phone'],
   			 'lianxiren_mail' => $values['lianxiren_mail'],
   			 'lianxiren_mobile' => $values['lianxiren_mobile'],
   			 'shifou_gaoxingqiye' => $values['shifou_gaoxingqiye'],
   			 'main_business' => $values['main_business'],
   			 'remark' => $values['remark'],
  			))->execute();
	  if ($last_id!=NULL) {
		 $form_state['redirect'] = 'beitouqiye/list/'.$last_id;
	  } else {
	    
	    // 数据写入失败了
	    drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
	    
	  }	
}

//传人被投企业id
function beitouqiye_exportcsv_perpage($qiye_id){
  $filename = '被投企业信息.csv' ;
  drupal_add_http_header('Content-Type', 'text/csv; charset=utf-8');
  drupal_add_http_header('Content-Disposition', 'attachment; filename="' . $filename . '"');
  $header = array(
      // iconv('utf-8','gb2312','序号'),
       iconv('utf-8','gb2312','被投企业名称'),
       iconv('utf-8','gb2312','投资时间'),
       iconv('utf-8','gb2312', '投资额（万元）'),
       iconv('utf-8','gb2312', '企业总股本数'),
       iconv('utf-8','gb2312', '收购股本数'),
       iconv('utf-8','gb2312', '占股比例'),
       
       iconv('utf-8','gb2312', '企业注册资本(万元)'),
       iconv('utf-8','gb2312', '企业所属地区'),
       iconv('utf-8','gb2312', '企业所属行业'),      
	   iconv('utf-8','gb2312', '设立时间'),
       iconv('utf-8','gb2312', '企业所属发展阶段'),
       iconv('utf-8','gb2312', '投资前一年度末总资产(万元)'),
       iconv('utf-8','gb2312', '投资前一年度末净资产(万元)'),
       iconv('utf-8','gb2312', '投资前一年度末销售收入(万元)'),
       iconv('utf-8','gb2312', '投资前一年度末净利润(万元)'),
       
       iconv('utf-8','gb2312', '企业联系人'),
       iconv('utf-8','gb2312', '电话'),
       iconv('utf-8','gb2312', '邮箱'),
       iconv('utf-8','gb2312', '手机'),
       
       iconv('utf-8','gb2312', '是否高新技术企业'),       
       iconv('utf-8','gb2312', '企业主营业务'),
       iconv('utf-8','gb2312', '备注'),
      );
  print implode(',', $header) ."\r\n";
  $query = db_select('beitouqiye_info', 'n')
      ->fields ('n', array (
      'id',
      'company_name',
      'touzi_timestamp',
      'touzi_money',
      'qiye_gubenshu',
      'shougou_gubenshu',
      'zhangu_percentage',
      
      'zuce_money',
      'locality',      
      'field',
      'sheli_timestamp',
      'fazhanjieduan',
      'qianyinian_allmoney',
      'qianyinian_jingmoney',
      'qianyinian_salemoney',
      'qianyinian_profitmoney',
      
      'lianxiren_name',
      'lianxiren_phone',      
	  'lianxiren_mail',
      'lianxiren_mobile',
      
      'shifou_gaoxingqiye',
      'main_business',
      'remark',      
      ))
	  ->condition('id',$qiye_id,'=');
  $results = $query
      ->execute();
  $serial_num = 1;
  foreach ($results as $node) {
      $touzi_timestamp = str_replace(array(',',",",'，',"，") , " ", format_date($node->touzi_timestamp, 'custom', 'D, Y-m-d'));
	  $sheli_timestamp = str_replace(array(',',",",'，',"，") , " ", format_date($node->sheli_timestamp, 'custom', 'D, Y-m-d'));
	  $shifou_gaoxingqiye = '否';
	  if($node->shifou_gaoxingqiye == 1){
	  	$shifou_gaoxingqiye = '是';
	  }
      $values = array( $node->company_name, $touzi_timestamp, $node->touzi_money, $node->qiye_gubenshu, 
        $node->shougou_gubenshu,$node->zhangu_percentage, $node->locality,$node->field, 
        $sheli_timestamp, $node->fazhanjieduan, $node->qianyinian_allmoney, $node->qianyinian_jingmoney, $node->qianyinian_salemoney,
		$node->qianyinian_profitmoney, $node->lianxiren_name, $node->lianxiren_phone, $node->lianxiren_mail, $node->lianxiren_mobile,
		$shifou_gaoxingqiye, $node->main_business, $node->remark); 
	  $str = implode(',', $values) ."\r\n";
	  $str = iconv('utf-8','gb2312',$str);
	  print $str;
	  //$serial_num+=1;
    }

  
}

function beitouqiye_exportcsv_page($fund_id){
  $filename = '被投企业信息.csv' ;
  drupal_add_http_header('Content-Type', 'text/csv; charset=utf-8');
  drupal_add_http_header('Content-Disposition', 'attachment; filename="' . $filename . '"');
  $header = array(
       iconv('utf-8','gb2312','序号'),
       iconv('utf-8','gb2312','被投企业名称'),
       iconv('utf-8','gb2312','投资时间'),
       iconv('utf-8','gb2312', '投资额（万元）'),
       iconv('utf-8','gb2312', '企业总股本数'),
       iconv('utf-8','gb2312', '收购股本数'),
       iconv('utf-8','gb2312', '占股比例'),
       
       iconv('utf-8','gb2312', '企业注册资本(万元)'),
       iconv('utf-8','gb2312', '企业所属地区'),
       iconv('utf-8','gb2312', '企业所属行业'),      
	   iconv('utf-8','gb2312', '设立时间'),
       iconv('utf-8','gb2312', '企业所属发展阶段'),
       iconv('utf-8','gb2312', '投资前一年度末总资产(万元)'),
       iconv('utf-8','gb2312', '投资前一年度末净资产(万元)'),
       iconv('utf-8','gb2312', '投资前一年度末销售收入(万元)'),
       iconv('utf-8','gb2312', '投资前一年度末净利润(万元)'),
       
       iconv('utf-8','gb2312', '企业联系人'),
       iconv('utf-8','gb2312', '电话'),
       iconv('utf-8','gb2312', '邮箱'),
       iconv('utf-8','gb2312', '手机'),
       
       iconv('utf-8','gb2312', '是否高新技术企业'),       
       iconv('utf-8','gb2312', '企业主营业务'),
       iconv('utf-8','gb2312', '备注'),
      );
  print implode(',', $header) ."\r\n";
  $query = db_select('beitouqiye_info', 'n')
      ->fields ('n', array (
      'id',
      'company_name',
      'touzi_timestamp',
      'touzi_money',
      'qiye_gubenshu',
      'shougou_gubenshu',
      'zhangu_percentage',
      
      'zuce_money',
      'locality',      
      'field',
      'sheli_timestamp',
      'fazhanjieduan',
      'qianyinian_allmoney',
      'qianyinian_jingmoney',
      'qianyinian_salemoney',
      'qianyinian_profitmoney',
      
      'lianxiren_name',
      'lianxiren_phone',      
	  'lianxiren_mail',
      'lianxiren_mobile',
      
      'shifou_gaoxingqiye',
      'main_business',
      'remark',      
      ))
	  ->condition('fund_id',$fund_id,'=');
  $results = $query
      ->execute();
  $serial_num = 1;
  foreach ($results as $node) {
      $touzi_timestamp = str_replace(array(',',",",'，',"，") , " ", format_date($node->touzi_timestamp, 'custom', 'D, Y-m-d'));
	  $sheli_timestamp = str_replace(array(',',",",'，',"，") , " ", format_date($node->sheli_timestamp, 'custom', 'D, Y-m-d'));
	  $shifou_gaoxingqiye = '否';
	  if($node->shifou_gaoxingqiye == 1){
	  	$shifou_gaoxingqiye = '是';
	  }
      $values = array($serial_num, $node->company_name, $touzi_timestamp, $node->touzi_money, $node->qiye_gubenshu, 
        $node->shougou_gubenshu,$node->zhangu_percentage, $node->locality,$node->field, 
        $sheli_timestamp, $node->fazhanjieduan, $node->qianyinian_allmoney, $node->qianyinian_jingmoney, $node->qianyinian_salemoney,
		$node->qianyinian_profitmoney, $node->lianxiren_name, $node->lianxiren_phone, $node->lianxiren_mail, $node->lianxiren_mobile,
		$shifou_gaoxingqiye, $node->main_business, $node->remark); 
	  $str = implode(',', $values) ."\r\n";
	  $str = iconv('utf-8','gb2312',$str);
	  print $str;
	  $serial_num+=1;
    }

  
}

//传人基金id
function beitouqiye_fundlist_perpage($fund_id) {
  $html = '';	
  $html .= l('返回基金列表','beitouqiye/fundlist/') .  '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'; 
  $html .= l('添加被投企业信息','beitouqiye/create/'.$fund_id) . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  $html .= l('导出','beitouqiye/exportcsv/'.$fund_id) . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  $options = array(
  'target_id' => 'printable_div',
  'value' => t('Print'),
  'type' => 'link',
  'custom_css' => drupal_get_path('module','my_module').'/css/print.css',
  );

  $html .= area_print_form($options). '<br />'. '<br />';
  
  $html .= '<div id="printable_div">';
  $round_num = 1;
  $header = array(
      array('data' => '序号'),
      array('data' => '被投企业名称', 'field' => 'company_name'),
      array('data' => '投资时间', 'field' => 'touzi_timestamp'),
      array('data' => '投资额(万元)', 'field' => 'touzi_money'),
      array('data' => '占股比例', 'field' => 'zhangu_percentage'),
      array('data' => '发展阶段', 'field' => 'fazhanjieduan'),
      array('data' => '详情'),
      );
  $query = db_select('beitouqiye_info', 'n')
      ->extend('PagerDefault') //Pager Extender
      ->limit(20)//20 results per page
      ->extend('TableSort')//Sorting Extender
      ->orderByHeader($header)//Field to sort on is picked from $header
      ->fields ('n', array (
      'id',
      'company_name',
      'touzi_timestamp',
      'touzi_money',
      'zhangu_percentage',
      'fazhanjieduan',
      ))
	  ->condition('fund_id',$fund_id,'=');
  $results = $query
      ->execute();

  $rows = array();
  $_serial_num = 1;
  foreach ($results as $node) {
      $rows[] = array(
      'data' => array(
        $_serial_num,
        l($node->company_name, 'beitouqiye/list/'. $node->id),
        format_date($node->touzi_timestamp, 'custom', 'D, Y-m-d'),
        $node->touzi_money,
        $node->zhangu_percentage,
        $node->fazhanjieduan,
         l('查看', 'beitouqiye/list/'. $node->id),
      )
      );
  }
  $html .= theme('table', 
      array(
            'header' => $header,
            'rows'=>$rows,
            //'caption' => '反馈意见列表',//Optional Caption for the table
            'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
            'empty' => '没有被投企业信息',//Optional empty text for the table if resultset is empty
      )
      );
  $html .= '</div>';
  $html .= theme('pager',
      array(
            'tags' => array()
      )
      );
  
  return ($html); 
}
//传人被投企业id
function beitouqiye_per_page($perid) {
 
  // content variable that will be returned for display
  $page_content = '';
  $query = "SELECT fund_id FROM " .
           "{beitouqiye_info} where id = ?";
  $queryResult = db_query($query, array($perid));
  foreach ($queryResult as $result) {
     $page_content .= l('返回企业列表','beitouqiye/fundlist/'.$result->fund_id) . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';     
  }
  $page_content .= l('导出','beitouqiye/exportcsv/per/'.$perid) . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  drupal_set_title('管理该项被投企业信息');
  $options = array(
  'target_id' => 'printable_div',
  'value' => t('Print'),
  'type' => 'link',
  'custom_css' => drupal_get_path('module','my_module').'/css/print.css',
  );

  $page_content .= area_print_form($options). '<br />'. '<br />';
  
  $page_content .= '<div id="printable_div">';
  	 
  $render_array = array(
	   '#markup' => $page_content,
	 );
	 
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
  $form = drupal_get_form('beitouqiye_edit_form',$perid);
  $render_array['#markup'] .= drupal_render($form);
  $render_array['#markup'] .= '</div>';
	 //Drupal7的页面回调，返回的应该是一个数组
  return $render_array;
}

function beitouqiye_edit_form($form, &$form_state, $perid){
  $query = "SELECT * FROM " .
           "{beitouqiye_info} where id = ?";
  $queryResult = db_query($query, array($perid));
  $company_name = '';   
  $touzi_timestamp = '';
  $touzi_money = '';
  $qiye_gubenshu = '';
  $shougou_gubenshu = '';
  $zhangu_percentage = '';
  $zuce_money = '';
  $locality = '';
  $field = '';
  $sheli_timestamp = '';
  $fazhanjieduan = '';
  $qianyinian_allmoney = '';
  $qianyinian_jingmoney = '';
  $qianyinian_salemoney = '';
  $qianyinian_profitmoney = ''; 
  $lianxiren_name = '';
  $lianxiren_phone = '';
  $lianxiren_mail = '';
  $lianxiren_mobile = '';
  $shifou_gaoxingqiye = '';
  $main_business = '';
  $remark = '';

  foreach ($queryResult as $links ) {
  	  $company_name = $links->company_name;   
	  $touzi_timestamp =  getdate($links->touzi_timestamp);
	  $touzi_money = $links->touzi_money;
	  $qiye_gubenshu = $links->qiye_gubenshu;
	  $shougou_gubenshu = $links->shougou_gubenshu;
	  $zhangu_percentage = $links->zhangu_percentage;
	  $zuce_money = $links->zuce_money;
	  $locality = $links->locality;
	  $field = $links->field;
	  $sheli_timestamp =  getdate($links->sheli_timestamp);
	  $fazhanjieduan = $links->fazhanjieduan;
	  $qianyinian_allmoney = $links->qianyinian_allmoney;
	  $qianyinian_jingmoney = $links->qianyinian_jingmoney;
	  $qianyinian_salemoney = $links->qianyinian_salemoney;
	  $qianyinian_profitmoney = $links->qianyinian_profitmoney; 
	  $lianxiren_name = $links->lianxiren_name;
	  $lianxiren_phone = $links->lianxiren_phone;
	  $lianxiren_mail = $links->lianxiren_mail;
	  $lianxiren_mobile = $links->lianxiren_mobile;
	  $shifou_gaoxingqiye = $links->shifou_gaoxingqiye;
	  $main_business = $links->main_business;
	  $remark = $links->remark;
  }
  // More coming....
  if ($queryResult == '') {
    $form = NULL;
  }
  else{
   $form['bid'] = array('#type' => 'hidden', '#value' => $perid);
    $form['company_name'] = array(
	  '#title' => t('被投企业名称'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['company_name'])?$_SESSION['beitouqiye_form']['company_name']:$company_name,
	 );
     $form['touzi_timestamp'] = array(
	     '#type' => 'date', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
	     '#title' => t('投资时间'),
	     '#default_value' => array('year' =>  $touzi_timestamp['year'], 'month' =>  $touzi_timestamp['mon'], 'day' => $touzi_timestamp['mday']), 
	 );
	 $form['touzi_money'] = array(
	  '#title' => t('投资额（万元）'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['touzi_money'])?$_SESSION['beitouqiye_form']['touzi_money']:$touzi_money,
	  '#description' => t('例如：250.55'),
	 ); 
	 $form['qiye_gubenshu'] = array(
	  '#title' => t('企业总股本数'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['qiye_gubenshu'])?$_SESSION['beitouqiye_form']['qiye_gubenshu']:$qiye_gubenshu,
	 ); 
	 $form['shougou_gubenshu'] = array(
	  '#title' => t('收购股本数'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['shougou_gubenshu'])?$_SESSION['beitouqiye_form']['shougou_gubenshu']:$shougou_gubenshu,
	 ); 
	 $form['zhangu_percentage'] = array(
	  '#title' => t('占股比例'),
	  '#type' => 'textfield',
	  '#required' => TRUE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['zhangu_percentage'])?$_SESSION['beitouqiye_form']['zhangu_percentage']:$zhangu_percentage,
	  '#description' => t('例如：16.8'),
	 ); 
	 //企业情况
	  $form['zuce_money'] = array(
	  '#title' => t('企业注册资本(万元)'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['zuce_money'])?$_SESSION['beitouqiye_form']['zuce_money']:$zuce_money,
	 );
	  $form['locality'] = array(
	  '#title' => t('企业所属地区'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['locality'])?$_SESSION['beitouqiye_form']['locality']:$locality,
	 );
	  $form['field'] = array(
	  '#title' => t('企业所属行业'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['field'])?$_SESSION['beitouqiye_form']['field']:$field,
	 );
     $form['sheli_timestamp'] = array(
	     '#type' => 'date', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
	     '#title' => t('设立时间'),
	     '#default_value' => array('year' =>  $sheli_timestamp['year'], 'month' =>  $sheli_timestamp['mon'], 'day' => $sheli_timestamp['mday']), 
	  );
	 $form['fazhanjieduan'] = array(
	  '#title' => t('企业所属发展阶段'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['fazhanjieduan'])?$_SESSION['beitouqiye_form']['fazhanjieduan']:$fazhanjieduan,
	 );
	 $form['qianyinian_allmoney'] = array(
	  '#title' => t('投资前一年度末总资产(万元)'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['qianyinian_allmoney'])?$_SESSION['beitouqiye_form']['qianyinian_allmoney']:$qianyinian_allmoney,
	 );	 
	 $form['qianyinian_jingmoney'] = array(
	  '#title' => t('投资前一年度末净资产(万元)'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['qianyinian_jingmoney'])?$_SESSION['beitouqiye_form']['qianyinian_jingmoney']:$qianyinian_jingmoney,
	 );
	 $form['qianyinian_salemoney'] = array(
	  '#title' => t('投资前一年度末销售收入(万元)'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['qianyinian_salemoney'])?$_SESSION['beitouqiye_form']['qianyinian_salemoney']:$qianyinian_salemoney,
	 );
	  $form['qianyinian_profitmoney'] = array(
	  '#title' => t('投资前一年度末净利润(万元)'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['qianyinian_profitmoney'])?$_SESSION['beitouqiye_form']['qianyinian_profitmoney']:$qianyinian_profitmoney,
	 );
     //企业联系人
	 $form['lianxiren_name'] = array(
	  '#title' => t('企业联系人'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['lianxiren_name'])?$_SESSION['beitouqiye_form']['lianxiren_name']:$lianxiren_name,
	 );	 
	 $form['lianxiren_phone'] = array(
	  '#title' => t('电话'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['lianxiren_phone'])?$_SESSION['beitouqiye_form']['lianxiren_phone']:$lianxiren_phone,
	 );
	 $form['lianxiren_mail'] = array(
	  '#title' => t('邮箱'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['lianxiren_mail'])?$_SESSION['beitouqiye_form']['lianxiren_mail']:$lianxiren_mail,
	 );
	  $form['lianxiren_mobile'] = array(
	  '#title' => t('手机'),
	  '#type' => 'textfield',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['lianxiren_mobile'])?$_SESSION['beitouqiye_form']['lianxiren_mobile']:$lianxiren_mobile,
	 );
	 $form["shifou_gaoxingqiye"] = array(
	    '#title' => t('是否高新技术企业'),
		"#type" => "checkbox",
		//"#prefix" => "<td>是否高新技术企业</td><td>",
		//"#suffix" => "</td><tr>",
		'#required' => FALSE,
	  	'#default_value' =>isset($_SESSION['beitouqiye_form']['shifou_gaoxingqiye'])?$_SESSION['beitouqiye_form']['shifou_gaoxingqiye']:$shifou_gaoxingqiye,
	);
	
  	 $form['main_business'] = array(
	  '#title' => t('企业主营业务'),
	  '#type' => 'textarea',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['main_business'])?$_SESSION['beitouqiye_form']['main_business']:$main_business,
	 );
	 $form['remark'] = array(
	  '#title' => t('备注'),
	  '#type' => 'textarea',
	  '#required' => FALSE,
	  '#default_value' =>isset($_SESSION['beitouqiye_form']['remark'])?$_SESSION['beitouqiye_form']['remark']:$remark,
	 );
	  $form['submit'] = array(
	    '#type' => 'submit',
	    '#value' => t('提交修改'),
	  );
	  
	 //表单元素“提交”按钮
	  $form['deleate'] = array(
	    '#type' => 'submit',
	    '#value' => t('删除该企业信息'),
	    '#submit' => array('beitouqiye_edit_form_deleate'),
	  );
  	
  }
  
  return $form;
}

function beitouqiye_edit_form_submit($form, &$form_state){
	  $values = NULL;
	  $values = $form_state['values'];
      $touzi_timestamp = mktime(0, 0, 0,$values['touzi_timestamp']['month'], $values['touzi_timestamp']['day'], $values['touzi_timestamp']['year']);
	  $sheli_timestamp = mktime(0, 0, 0,$values['sheli_timestamp']['month'], $values['sheli_timestamp']['day'], $values['sheli_timestamp']['year']);
	  db_update('beitouqiye_info')
  		->fields(array(
		 	 'company_name' => $values['company_name'],
             'touzi_timestamp' => $touzi_timestamp,
   			 'touzi_money' => $values['touzi_money'],
   			 'qiye_gubenshu' => $values['qiye_gubenshu'],
   			 'shougou_gubenshu' => $values['shougou_gubenshu'],
   			 'zhangu_percentage' => $values['zhangu_percentage'],		 
			 'zuce_money' => $values['zuce_money'],
   			 'locality' => $values['locality'],
   			 'field' => $values['field'],
             'sheli_timestamp' => $sheli_timestamp,  			 
   			 'fazhanjieduan' => $values['fazhanjieduan'],
   			 'qianyinian_allmoney' => $values['qianyinian_allmoney'],
   			 'qianyinian_jingmoney' => $values['qianyinian_jingmoney'],
   			 'qianyinian_salemoney' => $values['qianyinian_salemoney'],
   			 'qianyinian_profitmoney' => $values['qianyinian_profitmoney'],
   			 'lianxiren_name' => $values['lianxiren_name'],
   			 'lianxiren_phone' => $values['lianxiren_phone'],
   			 'lianxiren_mail' => $values['lianxiren_mail'],
   			 'lianxiren_mobile' => $values['lianxiren_mobile'],
   			 'shifou_gaoxingqiye' => $values['shifou_gaoxingqiye'],
   			 'main_business' => $values['main_business'],
   			 'remark' => $values['remark'],
  			))
  		->condition('id', $values['bid'])
  		->execute();
	 $form_state['redirect'] = 'beitouqiye/list/'.$values['bid'];
}

function beitouqiye_edit_form_deleate($form, &$form_state){
	 $values = NULL;
	 $_SESSION['beitouqiye_edit_form'] = $form_state['values'];
	 $values = $_SESSION['beitouqiye_edit_form'] ;
	 //表单重定向到确认页面
	 $form_state['redirect'] = 'beitouqiye/deleate/'.$values['bid'];
}

function beitouqiye_deleate_page($perid) {
  // content variable that will be returned for display
  $page_content = '删除后该被投企业信息将不可恢复！';
  
  drupal_set_title('删除该被投企业信息');
	 
	 $render_array = array(
	   '#markup' => $page_content,
	 );
	 
	 //该页面的正文为一个表单，注意对于表单，这里需要使用drupal_render呈现一下。
	 $form = drupal_get_form('beitouqiye_deleate_form');
	 $render_array['#markup'] .= drupal_render($form);
	 
	 //Drupal7的页面回调，返回的应该是一个数组
  return $render_array;

}

function beitouqiye_deleate_form($form, &$form_state){
 //表单元素“返回”按钮
  $form['back'] = array(
    '#type' => 'submit',
    '#value' => t('取消'),
    '#submit' => array('beitouqiye_deleate_form_back'),
  );
 //表单元素“提交”按钮
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('删除'),
  );
  
  return $form;
}

function beitouqiye_deleate_form_back($form, &$form_state){
	if(empty($_SESSION['beitouqiye_edit_form'])){
		  //drupal_goto('contact');
		  $form_state['redirect'] = 'beitouqiye/fundlist';
	  }else{
		  $values = $_SESSION['beitouqiye_edit_form'];
		  unset($_SESSION['beitouqiye_edit_form']);
		  $form_state['redirect'] = 'beitouqiye/list/'.$values['bid'];
	  }
}

function beitouqiye_deleate_form_submit($form, &$form_state){
  //简单的重定向到contactus页面
 if(empty($_SESSION['beitouqiye_edit_form'])){
		  //drupal_goto('contact');
		  $form_state['redirect'] = 'beitouqiye/fundlist';
	  }else{
		  $values = $_SESSION['beitouqiye_edit_form'];
		  unset($_SESSION['beitouqiye_edit_form']);
		  db_delete('beitouqiye_info')
			->condition('id' ,$values['bid'])
			->execute();
		  $form_state['redirect'] = 'beitouqiye/fundlist';
	  }

}

//被投企业设置基金选择入口
function beitouqiye_fundlist_page() {
 $html = '';
 
 $header = array(
      array('data' => '基金名称', 'field' => 'name', 'sort' => 'desc'),
      array('data' => '负责人', 'field' => 'principal_name'),
      array('data' => '成立时间', 'field' => 'timestamp'),
      array('data' => '已投企业基本资料设置', 'field' => 'beitouqiye_set'),
      //array('data' => '被投企业资本到位记录列表', 'field' => 'beitouqiye_set'),
      );
  $query = db_select('guiding_fund_info', 'n')
      ->extend('PagerDefault') //Pager Extender
      ->limit(15)//10 results per page
      ->extend('TableSort')//Sorting Extender
      ->orderByHeader($header)//Field to sort on is picked from $header
      ->fields ('n', array (
      'id',
      'name',
      'principal_name',
      'timestamp',
      ));
  $results = $query
      ->execute();

  $rows = array();
  foreach ($results as $node) {
          $title = l('进入', 'beitouqiye/fundlist/'.$node->id);
		  $rows[] = array(
	      'data' => array(
	        l($node->name, 'guidingfund/list/'. $node->id),
	        $node->principal_name,
	        format_date($node->timestamp, 'custom', 'D, Y-m-d'),
	        $title,
	        //l('资本到位', 'zibendaowei/jilu/ren_fund/'.$node->id),
	      )
	      );
  }
  $html .= theme('table', 
      array(
            'header' => $header,
            'rows'=>$rows,
            //'caption' => '反馈意见列表',//Optional Caption for the table
            'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
            'empty' => '没有基金信息',//Optional empty text for the table if resultset is empty
      )
      );
  $html .= theme('pager',
      array(
            'tags' => array()
      )
      );
  return ($html); 
}

//子基金投资项目管理基金选择入口
function touziyunxing_fundlist_page() {
 $html = '';
 
 $header = array(
      array('data' => '基金名称', 'field' => 'name'),
      array('data' => '已投企业管理'),
      array('data' => '投资协议章程管理'),
      array('data' => '重大事项管理'),
      array('data' => '年审计报告管理'),
      array('data' => '项目退出管理'),
      );
  $query = db_select('guiding_fund_info', 'n')
      ->extend('PagerDefault') //Pager Extender
      ->limit(15)//10 results per page
      ->extend('TableSort')//Sorting Extender
      ->orderByHeader($header)//Field to sort on is picked from $header
      ->fields ('n', array (
      'id',
      'name',
      'principal_name',
      'timestamp',
      ));
  $results = $query
      ->execute();

  $rows = array();
  foreach ($results as $node) {
		  $rows[] = array(
	      'data' => array(
	        l($node->name, 'guidingfund/list/'. $node->id),
	        l('进入', 'beitouqiye/fundlist/'.$node->id),
	        l('进入', 'touzixieyi_file/qiye/'.$node->id),
	        l('进入', 'qiye_zdsx_file/qiye/'.$node->id),
	        l('进入', 'qiye_sjbg_file/qiye/'.$node->id),
	        l('进入', 'qiye_tcbg_file/qiye/'.$node->id),
	      )
	      );
  }
  $html .= theme('table', 
      array(
            'header' => $header,
            'rows'=>$rows,
            //'caption' => '反馈意见列表',//Optional Caption for the table
            'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
            'empty' => '没有基金信息',//Optional empty text for the table if resultset is empty
      )
      );
  $html .= theme('pager',
      array(
            'tags' => array()
      )
      );
  return ($html); 
}

//基金退出分析统计基金选择入口
function exit_analysis_fundlist_page() {
 $html = '';
 
 $header = array(
      array('data' => '基金名称', 'field' => 'name'),
      array('data' => '已投项目数'),
      array('data' => '累计投资金额（万元）'),
      array('data' => '退出项目数'),
      array('data' => '退出总金额（万元）'),
      array('data' => '投资回报（万元）'),
      );
  $query = db_select('guiding_fund_info', 'n')
      ->extend('PagerDefault') //Pager Extender
      ->limit(15)//10 results per page
      ->extend('TableSort')//Sorting Extender
      ->orderByHeader($header)//Field to sort on is picked from $header
      ->fields ('n', array (
      'id',
      'name',
      ));
  $results = $query
      ->execute();

  $rows = array();
  foreach ($results as $node) {
  	  $invest_num = 0;
	  $invest_money = 0.0;
	  $exit_num = 0;
	  $exit_money = 0.0;
  	  $query = "SELECT touzi_money FROM " .
           "{beitouqiye_info} where fund_id = ?";
	  $queryResult = db_query($query, array($node->id));
	  foreach ($queryResult as $result) {
	  	 $invest_num++;
		 $invest_money += $result->touzi_money;
	  }
	  $query = "SELECT exit_money FROM " .
           "{company_exit_info} where fund_id = ?";
	  $queryResult = db_query($query, array($node->id));
	  foreach ($queryResult as $result) {
	  	 $exit_num++;
		 $exit_money += $result->exit_money;
	  }
	  $rows[] = array(
	      'data' => array(
	        l($node->name, 'guidingfund/list/'. $node->id),
	        l($invest_num, 'beitouqiye/fundlist/'.$node->id),
	        l($invest_money, 'beitouqiye/fundlist/'.$node->id),
	        l($exit_num, 'qiye_tcbg_file/qiye/'.$node->id),
	        l($exit_money, 'qiye_tcbg_file/qiye/'.$node->id),
	        $exit_money-$invest_money,
	      )
	      );
  }
  $html .= theme('table', 
      array(
            'header' => $header,
            'rows'=>$rows,
            //'caption' => '反馈意见列表',//Optional Caption for the table
            'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
            'empty' => '没有基金信息',//Optional empty text for the table if resultset is empty
      )
      );
  $html .= theme('pager',
      array(
            'tags' => array()
      )
      );
  return ($html); 
}
